package com.hspedu.houserent.view;

import com.hspedu.Override_.Exercise.main;
import com.hspedu.houserent.house.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utility.Utility;

import java.sql.SQLOutput;
import java.util.Scanner;

/**
 * 1.显示界面
 * 2.接受用户的输入
 * 3.调用HoseService完成对应的操作
 */
public class HouseView {
    private boolean loop = true; //控制显示菜单
    private char key = ' ';//接受用户选择菜单
    private HouseService houseService = new HouseService(10); //设置数组的大小为10

    //显示主菜单
    public void mainMenu() {
        do {
            System.out.println("-----------------房屋出租系统------------------");
            System.out.println("\t\t\t1 新 增 房 源");
            System.out.println("\t\t\t2 查 找 房 屋");
            System.out.println("\t\t\t3 删 除 房 屋");
            System.out.println("\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t6 退\t出");
            System.out.print("请输入你的选择(1~6):");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    readHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    quit();
                    break;
                default:
                    System.out.println("输入有误,请重新输入(1~6)");
                    break;
            }
        } while (loop);
    }


    //接受addHouse()的输入，创建House对象，调用add方法
    public void addHouse() {
        System.out.println("-------------------添加房屋-------------------");
        System.out.print("姓名:");
        String name = Utility.readString(8);
        System.out.print("电话:");
        String phone = Utility.readString(12);
        System.out.print("地址:");
        String address = Utility.readString(16);
        System.out.print("月租:");
        int rent = Utility.readInt();
        System.out.print("状态(未出租/已出租):");
        String state = Utility.readString(3, "未出租");
        //创建一个House对象
        //注意 id 是系统分配的，用户不能输入
        House newHouse = new House(0, name, phone, address, rent, state);
        if (houseService.add(newHouse)) {
            System.out.println("-------------------添加成功-------------------");
        } else {
            System.out.println("-------------------添加失败-------------------");
        }
    }


    //接受输入的id，查找该id的房屋信息
    public void readHouse() {
        System.out.println("-------------------查找房屋-------------------");
        System.out.print("请输入你要查找的id:");
        int searchId = Utility.readInt();
        House house = houseService.read(searchId);
        if (house != null) {
            System.out.println(house);
        } else {
            System.out.println("没有该房源信息");
            System.out.println("-------------------查找失败-------------------");
        }
    }


    //接受用户输入的信息，并创建一个house对象，然后交给HouseService进行处理
    public void updateHouse() {
        System.out.println("-------------------修改房屋-------------------");
        System.out.print("请选择待修改房屋编号(-1退出):");
        int updateId = Utility.readInt();

        if (updateId == -1) {
            System.out.println("-------------------放弃修改-------------------");
            return;
        }
        //先进行查找看是否有该id的房源信息
        House house = houseService.read(updateId);
        //返回的是引用类型，所以，对该数组的元素，进行改变会影响到该元素

        if (house != null) {
            //姓名
            System.out.print("姓名(" + house.getName() + "):");
            String name = Utility.readString(8, "");
            //这里如果用户直接回车表示不修改该信息，默认返回空串
            if (!"".equals(name)) {
                house.setName(name);
            }

            //电话
            System.out.print("电话(" + house.getPhone() + "):");
            String phone = Utility.readString(12, "");
            if (!"".equals(phone)) {
                house.setPhone(phone);
            }

            //地址
            System.out.print("地址(" + house.getAddress() + "):");
            String address = Utility.readString(16,"");
            if (!"".equals(address)){
                house.setAddress(address);
            }

            //租金
            System.out.print("租金(" + house.getRet() + "):");
            int rent = Utility.readInt(house.getRet());
            if (rent != house.getRet()){
                house.setRet(rent);
            }

            //状态
            System.out.print("状态(" + house.getState() + "):");
            String state = Utility.readString(3, "");
            if (!"".equals(state)){
                house.setState(state);
            }

            /*House newHouse = new House(0, name, phone, address, rent, state);
            houseService.update(updateId, newHouse);*/
            System.out.println("-------------------修改成功-------------------");
        } else {

            System.out.println("-------------------修改失败-------------------");
        }
    }


    //接受输入的的 id，查找为该id的房屋信息，如果有进行删除
    public void delHouse() {
        System.out.print("请选择待删除房屋编号(-1)退出:");
        int delId = Utility.readInt();

        //进行判断，查找该是否有id的信息
        if (delId == -1) {
            System.out.println("-------------------放弃删除-------------------");
            return;
        }

        char choice = Utility.readConfirmSelection();//该方法本身就有循环的逻辑
        if (choice == 'Y') {
            if (houseService.del(delId)) {
                System.out.println("-------------------删除完成-------------------");
            } else {
                System.out.println("-------------------删除失败-------------------");
            }

        } else {
            System.out.println("-------------------放弃删除-------------------");
            return;
        }
    }


    //显示房屋信息
    public void listHouse() {
        System.out.println("-------------------房屋列表-------------------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House[] houses = houseService.list();
        for (int i = 0; i < houses.length; i++) {
            if (houses[i] == null) {
                break;
            }
            System.out.println(houses[i]);
        }
        System.out.println("-------------------显示完毕-------------------");
    }

    //退出房租出租系统
    public void quit() {
        System.out.print("确认是否退出(Y/N):");
        char key = ' ';
        while (true) {
            key = Utility.readChar();
            if (key == 'y' || key == 'n' || key == 'Y' || key == 'N') {
                break;
            }
            System.out.print("输入有误，请重新输入(Y/N):");
        }

        if (key == 'Y' || key == 'y') {
            loop = false;
            System.out.println("你已退出房租出租系统,欢迎下次使用！");
            return;
        } else {
            System.out.println("程序继续运行~~");
        }
    }

}
